const Controllers = require('../controllers/Controller.js').controller;
const getMapDistance = require("../../utils/util").getMapDistance
const app = getApp();

Page({
  data: {
    globalStype:'',
  	inputValue:'',
    nearbyPagedata:[],
    pageNumber:1,
    pageSize:10,
    isEnd:false,
    lng:'',
    lat:'',
    isloading:true,
    ispostion:false,
  },

  onLoad(option) {
    this.setData({globalStype:app.globalData.globalStype})
  	this.setData({
        inputValue: option.search || ''
    })
    this.nearbyPage();
  },
  getaddress(e) {
  	var that=this;
    wx.getLocation({
        //定位类型 wgs84, gcj02
        type: 'gcj02',
        success: function(res) {
        	that.setData({
        		lng:res.longitude,
        		lat:res.latitude,
            ispostion:true
        	})
        },
        fail: function(err) {
            that.setData({
              ispostion:false
            })
        },
        complete: function(info) {
            that.nearbyPage()
        },
    })
  },
  nearbyPage(){
  	Controllers.nearbyPage({
		name:this.data.inputValue,
		pageSize:this.data.pageSize,
		pageNumber:this.data.pageNumber,
		// lng:this.data.lng,
		// lat:this.data.lat
  	}).then(res => {
		  if(res.data.list.length>0){
  			for(let item of res.data.list){
          if(item.label)
          item.label=item.label.split('/')
          if(item.lat && item.lng){
            item.distance=getMapDistance(this.data.lat,this.data.lng,item.lat,item.lng)
          }
    		}
        
        
      }
      this.setData({
        nearbyPagedata:this.data.nearbyPagedata.concat(res.data.list),
        isloading:false
      })
      if(res.data.lastPage){
          this.setData({
            isEnd:true
          })
        }
    });
  },

  onReachBottom() {
    if (this.data.isEnd) {
      return;
    } else {
      this.setData({
          pageNumber: this.data.pageNumber + 1,
      });
      this.nearbyPage()
    }
  },
  onShareAppMessage: function (e) {

  },
  onShareTimeline: function (e) {

  },
})